<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Article;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ArticleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Article(), function (Grid $grid) {
            $grid->model()->orderBy('is_top','desc')->orderBy('sort');
            $grid->column('title');
            $grid->column('image')->image('',50,50);

            $grid->column('status')->switch();
            $grid->column('is_recommend')->switch();
            $grid->column('is_popular')->switch();
            $grid->column('is_top')->switch();
            $grid->column('sort')->orderable();
            $grid->column('pubdate');
            $grid->quickSearch(['title']);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Article(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('image');
            $show->field('brief');
            $show->field('read_time');
            $show->field('read_volume');
            $show->field('word_number');
            $show->field('label');
            $show->field('content');
            $show->field('status');
            $show->field('sort');
            $show->field('is_recommend');
            $show->field('is_popular');
            $show->field('is_top');
            $show->field('pubdate');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Article(), function (Form $form) {

            $form->text('title')->required();
            $form->image('image')->autoUpload()->uniqueName()->move('article');
            $form->textarea('brief');
            $form->number('read_time')->help('分钟');
            $form->number('word_number');
            $form->editor('content');
            $form->text('label');
            $form->switch('status')->default(1);
            $form->switch('is_recommend');
            $form->switch('is_popular');
            $form->switch('is_top');
            $form->datetime('pubdate')->default(date('Y-m-d H:i:s'));
            $form->hidden('sort')->default(0);

        });
    }
}
